//
//  TicketSummaryCell.swift
//  ANOA
//
//  Created by 邱仙凯 on 2018/12/4.
//  Copyright © 2018 zwb. All rights reserved.
//

import UIKit

class TicketSummaryCell: BaseTableViewCell {
    lazy var  selBtn: UIButton = {
        let v = UIButton()
        v.layer.cornerRadius = 5
        v.backgroundColor = UIColor(kshex: "#4972F9")
        return v
    }()
    lazy var  deleteBtn: UIButton = {
        let v = UIButton()
        v.setImage(UIImage(named: "delete_item"), for: .normal)
        return v
    }()
    lazy var bg:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 8
        v.layer.masksToBounds = true
        return v
    }()
    lazy var cover:UIView = {
        let v = UIView()
        v.isUserInteractionEnabled = false
        v.isHidden = true
        v.backgroundColor = .white
        v.alpha = 0.6
        return v
    }()
    lazy var labLeft: UILabel = {
        let v = UILabel()
        v.font = UIFont.boldSystemFont(ofSize: 16)
        return v
    }()
    lazy var labMoney: UILabel = {
        let v = UILabel()
        v.font = UIFont.boldSystemFont(ofSize: 16)
        v.textColor = Theme.ORANGE
        return v
    }()
    lazy var labContent: UILabel = {
        let v = UILabel()
        v.numberOfLines = 0
        v.font = UIFont.systemFont(ofSize: 14)
        return v
    }()
    lazy var line: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex: "efefef")
        return v
    }()
    
    lazy var  btnCheckStatus: CheckVoButton = {
        let v = CheckVoButton()
        
        return v
    }()
    lazy var btnPdf: PDFButton =  {
        let v = PDFButton()
        return v
    }()
    
    private lazy var failReasonTitleLab: UILabel = {
        let v = UILabel()
        v.font = UIFont.boldSystemFont(ofSize: 16)
        return v
    }()
    

    var one = TicketModel(json: "") {
        didSet {
            labLeft.text = "\(one.fInvoicetypeNm.unwrap)"
            labMoney.text = "￥\(one.fTotalamount.unwrap)"
            
            var combineStr = ""
            if one.fInvoicedate.unwrap != "" {
                combineStr += "开票日期            \(one.fInvoicedate.unwrap)"
            }
            if one.fInvoiceno.unwrap != "" {
                combineStr += "\n发票号码            \(one.fInvoiceno.unwrap)"
            }
            if one.fBuyername.unwrap != "" {
                combineStr += "\n购买方                \(one.fBuyername.unwrap)"
            }
            if one.fLabel.unwrap != "" {
                combineStr += "\n标签                    \(one.fLabel.unwrap)"
            }
            if one.withSource {
                var danjuStr = ""
                if one.fCompany.intWrap == 1 {
                    danjuStr  = "\(one.sNo.unwrap)(\(one.vouType.unwrap))"
                } else {
                    danjuStr  = "\(one.inputNum.unwrap)(\(one.vouType.unwrap))"
                }
                if danjuStr != "()" {
                    combineStr += "\n所属单据            \(danjuStr)"
                }
 
                
            }
            
            btnCheckStatus.setCheckStatus(one: one)
            
            if btnCheckStatus.titleLabel?.text == "未查验" && one.fCheckFailMessage?.count ?? 0 > 0 {
                combineStr += "\n查验失败原因     \(one.fCheckFailMessage.unwrap)"
            }
            
            deleteBtn.isHidden = !(one.fUseState == "未报销")
            
            let tt = NSMutableAttributedString(string: combineStr)
            let colorText = [("开票日期", UIColor(kshex: "#8991B0")),
                             ("发票号码", UIColor(kshex: "#8991B0")),
                             ("购买方", UIColor(kshex: "#8991B0")),
                             ("标签", UIColor(kshex: "#8991B0")),
                             ("所属单据", UIColor(kshex: "#8991B0")),
                             ("查验失败原因", UIColor(kshex: "#8991B0"))]
            for item in colorText {
                tt.addAttribute(NSAttributedString.Key.foregroundColor, value: item.1, range: (combineStr as NSString).range(of: item.0))
                if item.0.contains("查验失败原因") && combineStr.contains("查验失败原因") {
                    let titleRange = (combineStr as NSString).range(of: item.0)
                    let redRange = NSRange(location: (titleRange.location + titleRange.length), length: combineStr.length - (titleRange.location + titleRange.length))
                    tt.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: redRange)
                }
            }
            let paraph = NSMutableParagraphStyle()
            //将行间距设置为
            paraph.lineSpacing = 12
    
            
            tt.addAttribute(NSAttributedString.Key.font, value:UIFont.systemFont(ofSize: 14), range: (combineStr as NSString).range(of: combineStr))
            tt.addAttribute(NSAttributedString.Key.paragraphStyle, value:paraph, range: (combineStr as NSString).range(of: combineStr))
            labContent.attributedText = tt
            
          
         

            
      
            
            if ifNeedPDF(one: one) {
                btnPdf.isHidden = false
                btnPdf.setHasPDF(flag: one.hasPdf.isSome)
           
            } else {
                btnPdf.isHidden = true
            }
            
            
            if one.withSelect {
                selBtn.isHidden = false
                if one.startSelect {
                    
                    
                    UIView.animate(withDuration: 0.5) {
                        self.labLeft.snp.updateConstraints { make in
                            make.left.equalToSuperview().offset(45)
                        }
                        self.labContent.snp.updateConstraints { make in
                            make.left.equalToSuperview().offset(45)
                        }
                        self.selBtn.snp.remakeConstraints { make in
                            make.width.equalTo(30)
                            make.height.equalTo(30)
                            make.left.equalToSuperview().offset(10)
                            make.centerY.equalToSuperview()
                        }
                    }
                    deleteBtn.isHidden = true
                    
                    
                    
                    self.cover.isHidden = one.selectable
                    self.selBtn.isUserInteractionEnabled = one.selectable
                    if one.selectable {
                        if one.isselected == 0 {
                            selBtn.setImage(nil, for: .normal)
                            selBtn.backgroundColor = UIColor(kshex: "#4972F9")
                        } else {
                            selBtn.setImage(UIImage(named:"check_invoice"), for: .normal)
                        }
                    } else {
                        selBtn.setImage(nil, for: .normal)
                        selBtn.layer.borderWidth = 1
                        selBtn.layer.borderColor = UIColor(kshex:"eeeeee").cgColor
                        selBtn.backgroundColor = .white

                    }
                    
                } else {
                    
                    UIView.animate(withDuration: 0.5) {
                        self.labLeft.snp.updateConstraints { make in
                            make.left.equalToSuperview().offset(8)
                         }
                        self.labContent.snp.updateConstraints { make in
                            make.left.equalToSuperview().offset(8)
                         }
                        self.selBtn.snp.remakeConstraints { make in
                            make.width.equalTo(27)
                            make.height.equalTo(27)
                            make.left.equalToSuperview().offset(-30)
                            make.centerY.equalToSuperview()
                        }
                    }
                    deleteBtn.isHidden = false
                    self.cover.isHidden = true
                }
                
                
            } else {
                selBtn.isHidden = true
                self.cover.isHidden = true

            }
            
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        
        
        contentView.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(6)
            make.top.equalTo(contentView)
            make.right.equalToSuperview().offset(-6)
            make.bottom.equalTo(contentView).offset(-6)
        }
        bg.addSubview(labLeft)
        bg.addSubview(labMoney)
        bg.addSubview(labContent)
        bg.addSubview(line)
        bg.addSubview(btnCheckStatus)
        bg.addSubview(btnPdf)
        bg.addSubview(selBtn)
        bg.addSubview(deleteBtn)
        bg.addSubview(cover)
        selBtn.snp.makeConstraints { make in
            make.width.equalTo(30)
            make.height.equalTo(30)
            make.left.equalToSuperview().offset(-30)
            make.centerY.equalToSuperview()
        }
        labLeft.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(8)
            make.top.equalTo(bg).offset(15)
        }
        labMoney.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            
            make.bottom.equalToSuperview().offset(-6)
        }
        line.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(8)
            make.right.equalToSuperview().offset(-8)
            make.height.equalTo(1)
            make.top.equalTo(labLeft.snp.bottom).offset(10)
            
        }
        labContent.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(8)
            make.left.equalToSuperview().offset(8)
            make.bottom.equalToSuperview().offset(-40)
            
            
        }
        
        deleteBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(8)
            make.height.equalTo(24)
            make.width.equalTo(25)
            make.centerY.equalTo(labMoney)
        }
        btnCheckStatus.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.height.equalTo(26)
            make.width.equalTo(70)
            make.centerY.equalTo(labLeft)
        }
        btnPdf.snp.makeConstraints { make in
            make.right.equalTo(btnCheckStatus.snp.left).offset(-8)
            make.height.equalTo(btnCheckStatus)
            make.width.equalTo(70)
            make.centerY.equalTo(labLeft)
        }
        cover.snp.makeConstraints { make in
            make.edges.equalTo(bg)
        }
    
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
class PDFButton:UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
            self.setTitle("pdf", for: .normal)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        self.layer.cornerRadius = 5
         self.setTitleColor(.white, for: .normal)

        
    }
    
    func setHasPDF(flag:Bool) {
        if flag {
            self.backgroundColor = UIColor(kshex: "#9076FF")
            self.setTitleColor(.white, for: .normal)

        } else {
            self.backgroundColor = UIColor(kshex: "#E5E8EE")
            self.setTitleColor(UIColor(kshex: "#8991B0"), for: .normal)
        
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class CheckVoButton:UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
            self.setTitle("pdf", for: .normal)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        self.layer.cornerRadius = 5
         self.setTitleColor(.white, for: .normal)

        
    }
    func setCheckStatus(one:TicketModel) {
    let ttS = checkStatusStr(one: one)
        self.setTitle(ttS, for: .normal)
            if one.fCheckstate == "1" {
                
                self.backgroundColor = UIColor(kshex: "#4972F9")
                self.setTitleColor(UIColor.white, for: .normal)
                
            } else if ifNeedCheck(one: one) {
            
              
                self.backgroundColor = UIColor(kshex: "#E5E8EE")
                self.setTitleColor(UIColor(kshex: "#8991B0"), for: .normal)
            
                
                
                if one.sCheckInfo.unwrap != "" {
                   
                    
                    self.backgroundColor = UIColor.red
                    self.setTitleColor(UIColor.white, for: .normal)
                
                }
                
            } else {
        
                self.backgroundColor = UIColor(kshex: "#F0F4F9")
                self.setTitleColor(UIColor(kshex: "#CACEE1"), for: .normal)
               
            }
        
       
     
        
    }
   
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
func checkStatusStr(one: TicketModel) -> String {
    if one.fCheckstate == "1" {
        
        return "已查验"
    } else if ifNeedCheck(one: one) {
        
         
        if one.sCheckInfo.unwrap != "" {
            
            return "查验失败"
        } else {
            return "未查验"
        }
        
    } else {
        
        return "不支持查验"
    }
}
